package hsp.admin.app.literature.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hsp.admin.app.literature.mapper.UpCountMapper;
import hsp.admin.app.literature.po.UpCountPO;
import hsp.admin.app.literature.service.IUpCountService;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UpCountServiceImpl extends ServiceImpl<UpCountMapper, UpCountPO> implements IUpCountService {

    @Override
    public void upCount(UpCountPO po) {
        UpCountPO upCountPO = getByLiteratureIdAndType(po.getLiteratureId(), po.getType());
        if(Objects.isNull(upCountPO)){
            upCountPO = new UpCountPO();
            upCountPO.setLiteratureId(po.getLiteratureId());
            upCountPO.setType(po.getType());
            upCountPO.setCount(1);
            save(upCountPO);
        }else{
            upCountPO.setCount(upCountPO.getCount() + 1);
            updateById(upCountPO);
        }
    }

    private UpCountPO getByLiteratureIdAndType(Long literatureId, String type) {
        LambdaQueryWrapper<UpCountPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UpCountPO::getLiteratureId, literatureId);
        wrapper.eq(UpCountPO::getType, type);
        return getOne(wrapper);
    }

    @Override
    public void deleteByLiteratureIdAndType(Long id, String type) {
        LambdaQueryWrapper<UpCountPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UpCountPO::getLiteratureId, id);
        wrapper.eq(UpCountPO::getType, type);
        remove(wrapper);
    }

}
